{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normal dictionary:\n",
      "a A\n",
      "b B\n",
      "c C\n",
      "d D\n"
     ]
    }
   ],
   "source": [
    "import collections\n",
    "\n",
    "# python 3.6 does track insertion order in normal dicts too!\n",
    "print('Normal dictionary:')\n",
    "\n",
    "d = {}\n",
    "d['a'] = 'A'\n",
    "d['b'] = 'B'\n",
    "d['c'] = 'C'\n",
    "d['d'] = 'D'\n",
    "\n",
    "for k,v in d.items():\n",
    "    print(k, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ordered Dict:\n",
      "a A\n",
      "b B\n",
      "c C\n"
     ]
    }
   ],
   "source": [
    "print('Ordered Dict:')\n",
    "od = collections.OrderedDict()\n",
    "od['a'] = 'A'\n",
    "od['b'] = 'B'\n",
    "od['c'] = 'C'\n",
    "\n",
    "for k,v in od.items():\n",
    "    print(k, v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Under python 3.6, normal dictionary also does insertion order tracking. This is not implemented in python 3.5\n",
    "\n",
    "Under python 3.5, the normal dict would have displayed the dict as - \n",
    "\n",
    "Normal dictionary:  \n",
    "d D  \n",
    "c C  \n",
    "b B  \n",
    "a A  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normal dictionary equality:  True\n"
     ]
    }
   ],
   "source": [
    "# Equality of dicts\n",
    "\n",
    "\n",
    "d1 = {'a':'A', 'b':'B', 'c':'C'}\n",
    "d2 = {'c':'C', 'b':'B', 'a':'A'}\n",
    "\n",
    "\n",
    "print('Normal dictionary equality: ', d1 == d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ordered dictionary equality:  False\n"
     ]
    }
   ],
   "source": [
    "d3 = collections.OrderedDict()\n",
    "\n",
    "d3['a'] = 'A'\n",
    "d3['b'] = 'B'\n",
    "d3['c'] = 'C'\n",
    "\n",
    "d4 = collections.OrderedDict()\n",
    "d4['c'] = 'C'\n",
    "d4['b'] = 'B'\n",
    "d4['a'] = 'A'\n",
    "\n",
    "\n",
    "print('Ordered dictionary equality: ', d3 == d4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before:\n",
      "a A\n",
      "b B\n",
      "c C\n",
      "d D\n",
      "\n",
      "After move_to_end():\n",
      "a A\n",
      "c C\n",
      "d D\n",
      "b B\n",
      "\n",
      "Setting move_to_end(last=False):\n",
      "b B\n",
      "a A\n",
      "c C\n",
      "d D\n"
     ]
    }
   ],
   "source": [
    "d = collections.OrderedDict(\n",
    "    [('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')]\n",
    ")\n",
    "\n",
    "print('Before:')\n",
    "for k, v in d.items():\n",
    "    print(k, v)\n",
    "\n",
    "d.move_to_end('b')\n",
    "\n",
    "print('\\nAfter move_to_end():')\n",
    "for k, v in d.items():\n",
    "    print(k, v)\n",
    "\n",
    "d.move_to_end('b', last=False)\n",
    "\n",
    "print('\\nSetting move_to_end(last=False):')\n",
    "for k, v in d.items():\n",
    "    print(k, v)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
